home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / vs / browser / cpb10ebx.exe / DATA.Z / cannon.java < prev    next >
Text File  |  1996-09-25  |  6KB  |  269 lines

  1. /*
  2.  *    File    : cannon.java (Circus Park 2 for Community Place)
  3.  *    Version : 2.0- 1996.09.10
  4.  *    Auther  : cori co.
  5.  *
  6.  *    Copyright(C) 1996 Sony Corporation. All rights reserved.
  7.  */
  8.  
  9. import vrml.*;
  10. import vrml.field.*;
  11. import vrml.node.*;
  12. import java.util.*;
  13. import vs.*;
  14.  
  15. public class cannon extends Script {
  16.  
  17.     static int EVENT_VOID = 0;
  18.     static int EVENT_BOOL = 1;
  19.     static int EVENT_TIME = 2;
  20.  
  21.     static int X = 0;
  22.     static int Y = 1;
  23.     static int Z = 2;
  24.     static int DEGREE = 3;
  25.  
  26.     float Radian = 180.0f / (float)Math.PI;
  27.  
  28.     /* Node */
  29.     SFNode CannonNode;
  30.  
  31.     /* EventOut */
  32.     SFBool        CannonSwitch;
  33.     SFVec3f        RocketmanPosition;
  34.     SFRotation    RocketmanRotation;
  35.  
  36.     /* sound success */
  37.     SFTime    CannonTrueSound_stop;
  38.     SFTime    CannonTrueSound_start;
  39.     /* sound failure */
  40.     SFTime    CannonFalseSound_stop;
  41.     SFTime    CannonFalseSound_start;
  42.     /* sound bullet start */
  43.     SFTime    CannonStartSound_stop;
  44.     SFTime    CannonStartSound_start;
  45.  
  46.     int CannonSwitchFlg = 0;
  47.     int CannonMoveCnt = -1;
  48.     float BulletAng;
  49.     float BulletOrgY = 0.0f;
  50.     float BulletOrgZ = -80.0f;
  51.     float BulletAccel;
  52.     float BulletY;
  53.     float BulletZ;
  54.  
  55.  
  56.     public void initialize() {
  57.         /* Node */
  58.         CannonNode                = (SFNode) getField( "CannonNode" );
  59.  
  60.         /* EventOut */
  61.         CannonSwitch            = (SFBool) getEventOut( "CannonTimer_enabled" );
  62.         RocketmanPosition        = (SFVec3f) getEventOut( "CannonRocketman_position" );
  63.         RocketmanRotation        = (SFRotation) getEventOut( "CannonRocketman_rotation" );
  64.  
  65.         /* sound success */
  66.         CannonTrueSound_stop    = (SFTime) getEventOut( "CannonTrueSound_stop" );
  67.         CannonTrueSound_start    = (SFTime) getEventOut( "CannonTrueSound_start" );
  68.         /* sound failure */
  69.         CannonFalseSound_stop    = (SFTime) getEventOut( "CannonFalseSound_stop" );
  70.         CannonFalseSound_start    = (SFTime) getEventOut( "CannonFalseSound_start" );
  71.         /* sound bullet start */
  72.         CannonStartSound_stop    = (SFTime) getEventOut( "CannonStartSound_stop" );
  73.         CannonStartSound_start    = (SFTime) getEventOut( "CannonStartSound_start" );
  74.     }
  75.  
  76.  
  77.     public void processEvent( Event e ) {
  78.         String name = e.getName();
  79.         double now = e.getTimeStamp();
  80.  
  81.         if ( name.equals( "CannonSw1Click" )) {
  82.             CannonSw1Click( (ConstSFBool)e.getValue(), now );
  83.         } else if ( name.equals( "CannonSw2Click" )) {
  84.             CannonSw2Click( (ConstSFBool)e.getValue(), now );
  85.         } else if ( name.equals( "CannonSw3Click" )) {
  86.             CannonSw3Click( (ConstSFBool)e.getValue(), now );
  87.         } else if ( name.equals( "CannonMove" )) {
  88.             CannonMove( (ConstSFTime)e.getValue(), now );
  89.         } else if ( name.equals( "CannonStartShare" )) {
  90.             CannonStartShare( (ConstSFString)e.getValue(), now );
  91.         } else {
  92.             ;
  93.         }
  94.     }
  95.  
  96.  
  97.     void cannon_init() {
  98.         float pos[] = new float[3];
  99.  
  100.         pos[X] = 0.0f;
  101.         pos[Y] = BulletOrgY;
  102.         pos[Z] = BulletOrgZ;
  103.         RocketmanPosition.setValue( pos );
  104.     }
  105.  
  106.  
  107.     /*
  108.      * sound on/off
  109.      */
  110.     public void soundSwitch( double now, boolean sw, boolean flg ) {
  111.         double time_st;
  112.         double time_ed;
  113.  
  114.         time_st = now;
  115.         time_ed = time_st + 5;
  116.         if ( sw == true ) {
  117.             if ( flg == true ) {
  118.                 CannonTrueSound_start.setValue( time_st );
  119.                 CannonTrueSound_stop.setValue( time_ed );
  120.             } else {
  121.                 CannonFalseSound_start.setValue( time_st );
  122.                 CannonFalseSound_stop.setValue( time_ed );
  123.             }
  124.         } else {
  125.             if ( flg == true ) {
  126.                 CannonTrueSound_stop.setValue(time_st);
  127.             } else {
  128.                 CannonFalseSound_stop.setValue(time_st);
  129.             }
  130.         }
  131.     }
  132.  
  133.  
  134.     /*
  135.      * switch1 clicked
  136.      */
  137.     public void CannonSw1Click ( ConstSFBool state, double now ) {
  138.         CannonSwitchChk ( state, now, 1 );
  139.     }
  140.  
  141.  
  142.     /*
  143.      * switch2 clicked
  144.      */
  145.     public void CannonSw2Click ( ConstSFBool state, double now ) {
  146.         CannonSwitchChk ( state, now, 2 );
  147.     }
  148.  
  149.  
  150.     /*
  151.      * switch3 clicked
  152.      */
  153.     public void CannonSw3Click ( ConstSFBool state, double now ) {
  154.         CannonSwitchChk ( state, now, 3 );
  155.     }
  156.  
  157.  
  158.     /*
  159.      * switch1-3 check
  160.      */
  161.     public void CannonSwitchChk ( ConstSFBool state, double now, int sw_num ) {
  162.         int val;
  163.         String s;
  164.  
  165.         if (state.getValue()) return;    /* mouse down */
  166.  
  167.         if ( CannonSwitchFlg >= 4 ) return;        /* game over */
  168.         if ( sw_num == CannonSwitchFlg ) return;    /* it is same button */
  169.  
  170.         val = ( sw_num - CannonSwitchFlg );
  171.         if ( val == 1 ) {                    /* true sw */
  172.             CannonSwitchFlg++;
  173.             soundSwitch( now, true, true );
  174.             if ( CannonSwitchFlg == 3 ) {
  175.                 CannonStart( now );            /* blast off the rocketman */
  176.                 Vscp.sendApplSpecificMsgWithDist( CannonNode, "CannonStartShare", "dummy", Vscp.allClientsExceptMe );
  177.             }
  178.         } else {                            /* false sw */
  179.             CannonSwitchFlg = 0;
  180.             soundSwitch( now, true, false );
  181.         }
  182.     }
  183.  
  184.  
  185.     public void CannonStartShare ( ConstSFString shared_data, double now ) {
  186.  
  187.         CannonStart( now );
  188.     }
  189.  
  190.  
  191.     /*
  192.      *
  193.      */
  194.     public void CannonStart ( double now ) {
  195.         double time;
  196.  
  197.         CannonSwitchFlg = 4;
  198.     
  199.         time = now;
  200.         CannonStartSound_start.setValue(time);
  201.         time += 10;
  202.         CannonStartSound_stop.setValue(time);
  203.  
  204.         CannonMoveCnt = 0;
  205.         BulletAng = 45.0f;
  206.         BulletY = BulletOrgY;
  207.         BulletZ = BulletOrgZ;
  208.         BulletAccel = 5.0f;
  209.  
  210.         CannonSwitch.setValue( true );
  211.     }
  212.  
  213.  
  214.     /*
  215.      * timer event
  216.      */
  217.     public void CannonMove ( ConstSFTime t, double now ) {
  218.         float pos[] = new float[3];
  219.         float rot[] = new float[4];
  220.         float angle_step, y_step, z_step;
  221.         String s;
  222.  
  223.         angle_step = 3.0f;
  224.         z_step = 5.0f;
  225.         y_step = z_step / (float)(30 / 2);
  226.         
  227.         if ( CannonMoveCnt < 0 ) {            /* init only */
  228.             CannonSwitch.setValue( false );
  229.             cannon_init();
  230.         } else if ( CannonMoveCnt < 30 ) {
  231.             rot[X] = 1.0f;    rot[Y] = 0.0f;    rot[Z] = 0.0f;
  232.             rot[DEGREE] = BulletAng / Radian;
  233.             RocketmanRotation.setValue( rot );
  234.             BulletAng += angle_step;
  235.             
  236.             pos[X] = 0.0f;
  237.             pos[Y] = BulletY;
  238.             pos[Z] = BulletZ + 10.0f;
  239.             RocketmanPosition.setValue( pos );
  240.             BulletZ += z_step;
  241.             BulletY += BulletAccel;
  242.             BulletAccel -= y_step;
  243.         } else if ( CannonMoveCnt == 30 ) {
  244.             rot[X] = 1.0f;    rot[Y] = 1.0f;    rot[Z] = 1.0f;    rot[DEGREE] = 0.0f;
  245.             RocketmanRotation.setValue( rot );
  246.             BulletY = BulletOrgY;
  247.         } else if ( CannonMoveCnt < 60 ) {
  248.             pos[X] = 0.0f;
  249.             pos[Y] = BulletY;
  250.             pos[Z] = BulletZ;
  251.             RocketmanPosition.setValue( pos );
  252.             BulletZ -= z_step;
  253.         } else if ( CannonMoveCnt == 60 ) {
  254.             BulletZ = BulletOrgZ;
  255.             pos[X] = 0.0f;
  256.             pos[Y] = BulletY;
  257.             pos[Z] = BulletZ;
  258.             RocketmanPosition.setValue( pos );
  259.             CannonSwitchFlg = 0;
  260.             CannonSwitch.setValue( false );
  261.         }
  262.         CannonMoveCnt++;
  263.     }
  264.  
  265. }
  266.  
  267.  
  268.  
  269.